package com.training.mapper;

import com.training.entity.ShoppingCart;
import com.training.entity.Course; // To retrieve course details in cart
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Options;
// import org.apache.ibatis.annotations.Delete; // 移除Delete注解，我们会在XML中定义SQL

import java.util.List;

@Mapper
public interface ShoppingCartMapper {

    /**
     * Find all non-logically deleted items in a student's shopping cart.
     * Joins with the course table to get course details.
     * @param studentId Student ID
     * @return List of Course objects in the cart
     */
    List<Course> getCartItemsByStudentId(@Param("studentId") Long studentId);

    /**
     * Find a specific cart item by student ID and course ID.
     * @param studentId Student ID
     * @param courseId Course ID
     * @return ShoppingCart object if found, null otherwise
     */
    ShoppingCart getCartItemByStudentIdAndCourseId(@Param("studentId") Long studentId, @Param("courseId") Long courseId);

    /**
     * Insert a new item into the shopping cart.
     * @param shoppingCart ShoppingCart object to insert
     * @return Number of affected rows
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addToCart(ShoppingCart shoppingCart);

    /**
     * Update an existing item in the shopping cart (e.g., quantity).
     * @param shoppingCart ShoppingCart object to update
     * @return Number of affected rows
     */
    int updateShoppingCartItem(ShoppingCart shoppingCart);

    /**
     * Logically delete an item from the shopping cart by its primary ID.
     * @param id ShoppingCart item ID
     * @return Number of affected rows
     */
    // 原有的逻辑删除，这里假设是更新 `deleted` 字段
    int deleteShoppingCartItem(@Param("id") Long id);

    /**
     * Logically delete all items from a student's shopping cart.
     * Used after successful purchase.
     * @param studentId Student ID
     * @return Number of affected rows
     */
    // 原有的清空购物车方法，这里假设是更新 `deleted` 字段
    int clearShoppingCartByStudentId(@Param("studentId") Long studentId);

    /**
     * 根据学生ID和课程ID逻辑删除购物车中的特定课程项。
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 受影响的行数
     */
    // 假设是更新 `deleted` 字段为 1。该方法将通过XML映射实现。
    int deleteCartItemByStudentIdAndCourseId(@Param("studentId") Long studentId, @Param("courseId") Long courseId);

    /**
     * 物理删除购物车项目。
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 受影响的行数
     */
    int physicalDeleteCartItemByStudentIdAndCourseId(@Param("studentId") Long studentId, @Param("courseId") Long courseId);

    /**
     * 物理清空学生购物车中的所有项目。
     * @param studentId 学生ID
     * @return 受影响的行数
     */
    int physicalClearCartByStudentId(@Param("studentId") Long studentId);
}